package com.pachong.android.framework.citypicker;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.pachong.android.framework.R;
import com.pachong.android.frameworkbase.customviews.WheelView;
import com.pachong.android.frameworkbase.dialog.base.BottomDialogFragment;

import java.util.List;

/**
 * 城市选择对话框
 *
 *
 * @Author: PengWeiW
 * @E-mail: pengveivei@sina.cn
 * @CreateDate: 2016-12-09
 * @Version: 1.0.0
 */
public class ChooseCityDialogFragment extends BottomDialogFragment implements View.OnClickListener {

    private OnCitySelectedListener mOnCitySelectedListener;
    private WheelView mWheelViewProvince;
    private WheelView mWheelViewCity;
    private WheelView mWheelViewCountry;

    private CityDataSource mCityDataSource;
    private int mCurrProvinceIndex = -1;
    private int mCurrCityIndex = -1;
    private int mCurrCountryIndex = -1;



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_choose_city_dialog, container);
        mCityDataSource = new CityDataSource(getContext());

        TextView btnCannel = (TextView) view.findViewById(R.id.btn_cancel);
        btnCannel.setOnClickListener(this);
        TextView btnEnter = (TextView) view.findViewById(R.id.btn_enter);
        btnEnter.setOnClickListener(this);

        mWheelViewProvince = (WheelView) view.findViewById(R.id.wvProvince);
        mWheelViewCity = (WheelView) view.findViewById(R.id.wvCity);
        mWheelViewCountry = (WheelView) view.findViewById(R.id.wvCounty);

        if (mWheelViewCountry.getListSize() == 0) {
            mWheelViewCountry.setEnable(false);
        } else {
            mWheelViewCountry.setEnable(true);
        }


        mWheelViewCountry.setOnSelectListener(new WheelView.OnSelectListener() {
            //县级监听
            @Override
            public void endSelect(int id, String text) {
                if (mWheelViewCountry.getListSize() == 0) {
                    return;
                }
                if (TextUtils.isEmpty(text)) {
                    return;
                }
                if (mCurrCountryIndex != id) {
                    mCurrCountryIndex = id;
                    String selectCity = (String) mWheelViewCountry.getData().get(mCurrCountryIndex);
                    if (TextUtils.isEmpty(selectCity)) {
                        return;
                    }
                    int lastIndex = Integer.valueOf(mWheelViewCountry.getListSize());
                    if (id > lastIndex) {
                        mWheelViewCountry.setDefault(lastIndex - 1);
                    }
                }
            }

            @Override
            public void selecting(int id, String text) {

            }
        });
        mWheelViewCity.setOnSelectListener(new WheelView.OnSelectListener() {
            //市级监听
            @Override
            public void endSelect(int index, String text) {
                if (TextUtils.isEmpty(text)) {
                    return;
                }
                mCurrCityIndex = index;
                String selectedCity = (String)mWheelViewCity.getData().get(index);
                List<String> countyList = mCityDataSource.getCountyNameList(selectedCity); //获得指定的县级名称
                if (countyList.size() > 0) {
                    mWheelViewCountry.setData(countyList);
                    mWheelViewCountry.setDefault(0);
                    mWheelViewCountry.setEnable(true);
                    return;

                } else { // 不存在县级列表，强制置空以防崩溃 ,自定义控件的bug,没对空数据的绘制处理
                    countyList.add("");
                    mWheelViewCountry.setData(countyList);
                    mWheelViewCountry.setEnable(false);
                    mWheelViewCountry.setDefault(0);
                }
            }

            @Override
            public void selecting(int id, String text) {
            }
        });
        mWheelViewProvince.setOnSelectListener(new WheelView.OnSelectListener() {
            //省级监听
            @Override
            public void endSelect(int index, String text) {
                if (TextUtils.isEmpty(text)) {
                    return;
                }
                if (mCurrProvinceIndex != index) {
                    mCurrProvinceIndex = index;
                    // fix bug: 参数中的text是被处理的过的。 “text="内蒙古..."”， 而实际上我们的数据是"内蒙古自治区"
                    String selectedProvince = (String)mWheelViewProvince.getData().get(index);
                    List<String> city = mCityDataSource.getCityNameList(selectedProvince);
                    if (city.size() == 0) {
                        return;
                    }
                    mWheelViewCity.setData(city);
                    if (city.size() > 1) {
                        //if city is more than one,show start index == 1
                        mWheelViewCity.setDefault(1);
                    } else {
                        mWheelViewCity.setDefault(0);
                    }
                }
            }

            @Override
            public void selecting(int id, String text) {

            }
        });
        //加载省份List
        mWheelViewProvince.setData(mCityDataSource.getProvinceNameList());
        mWheelViewProvince.setDefault(0);

        //加载城市List
        String defaultProvince = mCityDataSource.getProvinceNameList().get(0);
        mWheelViewCity.setData(mCityDataSource.getCityNameList(defaultProvince));
        mWheelViewCity.setDefault(1);

        //加载县级List
        List<String> cityList = mCityDataSource.getCityNameList(defaultProvince);
        mWheelViewCountry.setData(mCityDataSource.getCountyNameList(cityList.get(1)));
        mWheelViewCountry.setDefault(0);
        setCanceledOnTouchOutside(true);
        return view;
    }

    /**
     * 获取被用户选中的“省份”
     * @return
     */
    public String getSelectedProvinceName() {
        if (mWheelViewProvince == null|| mCurrProvinceIndex < 0) {
            return null;
        }
        return (String) mWheelViewProvince.getData().get(mCurrProvinceIndex);
    }

    /**
     * 获取被用户选中的“区/乡镇”
     * @return
     */
    public String getSelectedCountryName() {
        if (mWheelViewCountry == null || mCurrCountryIndex < 0) {
            return null;
        }
        return (String) mWheelViewCountry.getData().get(mCurrCountryIndex);
    }

    /**
     * 获取被用户选中的“城市”
     * @return
     */
    public String getSelectedCityName() {
        if (mWheelViewCity == null|| mCurrCityIndex < 0) {
            return null;
        }
        return (String) mWheelViewCity.getData().get(mCurrCityIndex);
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.btn_cancel) {
            dismiss();

        } else if (id == R.id.btn_enter) {
            Province province = new Province();
            Province.City city = new Province.City();
            Province.Country country = new Province.Country();

            mCityDataSource.findIds(getSelectedProvinceName(), province, getSelectedCityName(), city, getSelectedCountryName(), country);

            mOnCitySelectedListener.onSelected(province, city, country);
            dismiss();
        }
    }

    public void setOnCitySelectedListener(OnCitySelectedListener listener) {
        this.mOnCitySelectedListener = listener;
    }

    public interface OnCitySelectedListener {
        /**
         * @param province 省份 e.g 福建省
         * @param city     城市 e.g 福州市
         * @param country   区/乡镇  e.g 集美
         */
        void onSelected(Province province, Province.City city, Province.Country country);
    }
}
